/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By 冻结资金管理.yixiang.co
* 注意：
* 本软件为冻结资金管理.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.mall.biz.freeze.controller;
import java.util.Arrays;

import co.yixiang.common.log.annotation.Log;
import co.yixiang.common.mproot.domain.PageResult;
import co.yixiang.common.mproot.dozer.service.IGenerator;
import co.yixiang.mall.biz.freeze.service.TbUserRebateFreezeService;
import co.yixiang.mall.common.order.domain.TbUserRebateFreeze;
import co.yixiang.mall.common.order.dto.TbUserRebateFreezeDto;
import co.yixiang.mall.common.order.dto.TbUserRebateFreezeQueryCriteria;
import lombok.AllArgsConstructor;

import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author zhuhongjian
* @date 2021-03-19
*/
@AllArgsConstructor
@Api(tags = "冻结资金管理管理")
@RestController
@RequestMapping("/api/tbUserRebateFreeze")
@EnableScheduling
public class TbUserRebateFreezeController {

    private final TbUserRebateFreezeService tbUserRebateFreezeService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("hasAnyAuthority('admin','tbUserRebateFreeze:list')")
    public void download(HttpServletResponse response, TbUserRebateFreezeQueryCriteria criteria) throws IOException {
        tbUserRebateFreezeService.download(generator.convert(tbUserRebateFreezeService.queryAll(criteria), TbUserRebateFreezeDto.class), response);
    }

    @GetMapping
    @Log("查询冻结资金管理")
    @ApiOperation("查询冻结资金管理")
    @PreAuthorize("hasAnyAuthority('admin','tbUserRebateFreeze:list')")
    public ResponseEntity<PageResult<TbUserRebateFreezeDto>> getTbUserRebateFreezes(TbUserRebateFreezeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tbUserRebateFreezeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增冻结资金管理")
    @ApiOperation("新增冻结资金管理")
    @PreAuthorize("hasAnyAuthority('admin','tbUserRebateFreeze:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody TbUserRebateFreeze resources){
        return new ResponseEntity<>(tbUserRebateFreezeService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改冻结资金管理")
    @ApiOperation("修改冻结资金管理")
    @PreAuthorize("hasAnyAuthority('admin','tbUserRebateFreeze:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody TbUserRebateFreeze resources){
        tbUserRebateFreezeService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除冻结资金管理")
    @ApiOperation("删除冻结资金管理")
    @PreAuthorize("hasAnyAuthority('admin','tbUserRebateFreeze:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        Arrays.asList(ids).forEach(id->{
            tbUserRebateFreezeService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("冻结资金解冻")
    @Transactional
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void UnfreezeTask(){
        tbUserRebateFreezeService.unfreezeMoney();
    }
}
